package five_class_design_creational_patterns.four_SingletonPattern;

import java.util.ArrayList;
import java.util.Random;

/**
 * @author 北巷_东坡
 * @date 2022/10/16 8:48
 * @computer 10741
 */
public class Singieton_LDP {
    private static ArrayList<Singieton_LDP> singieton_ldpArrayList = new ArrayList<>() ;

    private Singieton_LDP(){}

    synchronized public static Singieton_LDP getInstance() {
        if (singieton_ldpArrayList.size() < 3) {
            System.out.println("成功创建新实例");
            Singieton_LDP singieton_ldp = new Singieton_LDP();
            singieton_ldpArrayList.add(singieton_ldp);
            return singieton_ldp;
        }else {
            System.out.println("不能创建多于3个实例");
            return singieton_ldpArrayList.get((new Random().nextInt(singieton_ldpArrayList.size())));
        }
    }
}
